using System;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using UserInterface.MML;
using Domain.Entities;
using UserInterface.Misc;
using Domain.Permissions;

namespace UserInterface.UserControls
{
    public partial class MessageView : UserControl
    {
        private Message message;
        private bool displayForumTopicInfo = false;

        public Message Message
        {
            get { return message; }
            set { message = value; }
        }

        public bool DisplayForumTopicInfo
        {
            get { return displayForumTopicInfo; }
            set { displayForumTopicInfo = value; }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            Session session = SessionManager.CurrentSession;
            User user = session.User;
            Topic topic = message.Topic;
            Forum forum = topic.Forum;
            PermissionManager permissionManager = new PermissionManager(user);
            ForumPermissionManager forumPermissionManager = permissionManager.CreateForumPermissionManager(forum);

            AttachmentsPlaceHolder.Visible = message.Attachments.Count > 0;
            AttachmentsRepeater.DataSource = message.Attachments;
            AttachmentsRepeater.DataBind();

            UserOnlyContentPlaceHolder.Visible = !(new PermissionManager(message.Poster)).IsGuest();
            PMLinkPlaceHolder.Visible = message.Poster.Id != UITools.GuestUserId;

            if (message.Poster.AvatarUrl == String.Empty)
            {
                AvatarImage.ImageUrl = "~/Templates/" + session.CurrentTemplate + "/Images/undefined.gif";
            }
            else
            {
                AvatarImage.ImageUrl = message.Poster.AvatarUrl;
            }

            EditOptionPlaceHolder.Visible = !session.IsGuest
                && (!topic.Locked || forumPermissionManager.Moderate)
                && (forumPermissionManager.Moderate
                    || (forumPermissionManager.EditOwn
                        && message.Poster.Id == session.UserId));

            QuoteOptionPlaceHolder.Visible = !topic.Locked && forumPermissionManager.Reply;

            ForumTopicInfoPlaceHolder.Visible = displayForumTopicInfo;
        }

        protected string TextFiltered
        {
            get { return Manager.Process(message.Text, message); }
        }

        protected string MessageClass
        {
            get { return message.ModeratorsOnly ? "moderator-message" : "normal-message"; }
        }
    }
}